'use strict';
angular.module('clientApp')

/**
 * @ngdoc function
 * @name clientApp.controller:GestionVols
 * @description
 * # AboutCtrl
 * Controller of the clientApp
 */
.controller('GestionVols', function ($scope, $rootScope, $location, ngToast, $modal) {
	$scope.volSelected = null;
	$scope.volAdded = null;
	$scope.rafraichirRecherche = false;
	$scope.data = [];
	
	if ($rootScope.user === null) {
		ngToast.create({
			content: 'Vous devez vous connecter pour accéder à cette fonction',
			className: 'info'
		});
		$location.path('/connection');
    }
	
	$scope.viewTab = 'tab1';
	$scope.changeTab = function(tab) {
	    $scope.viewTab = tab;
	    if (tab === 'tab2' && $scope.volSelected) {
	    	$scope.volSelected = null;
	    }
	};
	
	$scope.$watch('volAdded', function() {
		if ($scope.volAdded) {
			$scope.data.push($scope.volAdded);
			$scope.volAdded = null;
			$scope.changeTab('tab1');
		}
	});
	
	$scope.open = function (size) {
		var modalInstance = $modal.open({
			templateUrl: 'views/modalSupprimerVol.html',
			controller: 'supprimerVolModalController',
			size: size,
			resolve: {
				vol: function () {
					return $scope.volSelected;
				}
			}
		});
		
		modalInstance.result.then(function (vol) {
			if (vol) {
				$scope.data.pop(vol);
				$scope.volSelected = null;
				$scope.changeTab('tab1');
			}
		});
	};
});
